package com.sangananitsolution.gogoproject;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class SignUp extends Activity {

	Context context;

	ImageView backbutton, saveinfo;
	EditText etfirstname, etlastname, etemailadress, etpasword, etphonenuymber,
			etcountry;
	String issuccess;

	String firstname, lastname, emailaddress, password, phone, counrty;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.signuppage);
		backbutton = (ImageView) findViewById(R.id.imgbackarroronsignup);
		saveinfo = (ImageView) findViewById(R.id.imgjoin);
		etfirstname = (EditText) findViewById(R.id.etfirstname);

		etlastname = (EditText) findViewById(R.id.etlastname);
		etemailadress = (EditText) findViewById(R.id.etemail);

		etphonenuymber = (EditText) findViewById(R.id.etphone);
		etpasword=(EditText) findViewById(R.id.etpassword);
		etcountry = (EditText) findViewById(R.id.etcountryfav);
		backbutton.setOnClickListener(new OnClickListener() {

			@Override
			
			public void onClick(View arg0) {
				// Move to previous page

				startActivity(new Intent(SignUp.this, MainActivity.class));

			}
		});

		saveinfo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				if (etfirstname.getText().length() != 0
						&& etfirstname.getText().toString() != "") {
					if (etemailadress.getText().length() != 0
							&& etemailadress.getText().toString() != "") {
						if (etpasword.getText().length() != 0
								&& etpasword.getText().toString() != "") {

							if (etphonenuymber.getText().length() != 0
									&& etphonenuymber.getText().toString() != "") {
								if (etcountry.getText().length() != 0
										&& etcountry.getText().toString() != "") {

									firstname = etfirstname.getText()
											.toString();
									lastname = etlastname.getText().toString();
									emailaddress = etemailadress.getText()
											.toString();
									password = etpasword.getText().toString();
									phone = etphonenuymber.getText().toString();
									counrty = etcountry.getText().toString();

									new AdduesrAsyntask().execute();

								} else {
									Toast.makeText(getApplicationContext(),
											"fill country name ", Toast.LENGTH_LONG).show();

								}

							} else {
								Toast.makeText(getApplicationContext(),
										"fill phone number ", Toast.LENGTH_LONG).show();
							}

						} else {
							Toast.makeText(getApplicationContext(),
									"fill Password ", Toast.LENGTH_LONG).show();

						}

					} else {
						Toast.makeText(getApplicationContext(),
								"fill Email Address", Toast.LENGTH_LONG).show();

					}

				} else {
					Toast.makeText(getApplicationContext(), "fill First Name",
							Toast.LENGTH_LONG).show();
				}

			}

		});
		
		
		

	}

	public void getPhoto(View view) {
		// Move to uploading image from camera and gallery

		
		Toast.makeText(getApplicationContext(), "fill First Name",
				Toast.LENGTH_LONG).show();
		/*startActivity(new Intent(SignUp.this,
				PhotoHandlingClass.class));	*/
	}
	private class AdduesrAsyntask extends AsyncTask<String, String, Void> {

		private final String URL = "http://garagesalesagogo.com.au/AuthenticationService.asmx?op=AddUser";
		private final String SOAP_ACTION = "http://tempuri.org/AddUser";
		private String TAG = "PGGURU";

		@Override
		protected Void doInBackground(String... params) {
			Log.i(TAG, "doInBackground");
			SoapObject adduser = null;
			SoapObject usrobj = null;
			SoapObject loactionobj = null;

			// Create request

			adduser = new SoapObject("http://tempuri.org/", "AddUser");

			usrobj = new SoapObject("http://tempuri.org/", "usr");
			usrobj.addProperty("EmailAddress", emailaddress);
			usrobj.addProperty("Passsword", password);
			usrobj.addProperty("FirstName", firstname);
			usrobj.addProperty("LastName", lastname);
			usrobj.addProperty("PhoneNumber", phone);
			usrobj.addProperty("Country", counrty);

			loactionobj = new SoapObject("http://tempuri.org/", "Location");
			loactionobj.addProperty("Longitude", "125.21");
			loactionobj.addProperty("Latitude", "202.2");
			usrobj.addSoapObject(loactionobj);

			usrobj.addProperty("UserImage", "nkndnd");
			usrobj.addProperty("FacebookId", "njns442");
			adduser.addSoapObject(usrobj);

			// Create envelope
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.dotNet = true;
			// Set output SOAP object
			envelope.setOutputSoapObject(adduser);
			envelope.implicitTypes = true;
			// Create HTTP call object
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
			SoapObject adduesrResponseobj = null;
			SoapObject adduesrResultobj = null;
			SoapObject userdateobj = null;

			try {
				// Invole web service
				androidHttpTransport.call(SOAP_ACTION, envelope);
				// Get the response

				adduesrResponseobj = (SoapObject) envelope.bodyIn;
				adduesrResultobj = (SoapObject) adduesrResponseobj
						.getProperty(0);
				issuccess = adduesrResultobj.getPropertyAsString("IsSuccess");
				String message = adduesrResultobj
						.getPropertyAsString("Message");
				userdateobj = (SoapObject) adduesrResultobj
						.getProperty("UserData");
				String emailaddress = userdateobj
						.getPropertyAsString("EmailAddress");

				String firstbane = userdateobj.getPropertyAsString("FirstName");

				Log.i(TAG, issuccess);
				Log.i(TAG, message);
				Log.i(TAG, emailaddress);
				Log.i(TAG, firstbane);

			} catch (Exception e) {
				e.printStackTrace();
			}

			return null;
		}

		@Override
		protected void onPreExecute() {
			Log.i(TAG, "onPreExecute");
			

		}

		@Override
		protected void onProgressUpdate(String... values) {
			Log.i(TAG, "onProgressUpdate");

		}

		@Override
		protected void onPostExecute(Void result) {
			Log.i(TAG, "onPostExecute");

			if (issuccess.equals("true")) {

				Toast.makeText(getApplicationContext(), "Data is saved",
						Toast.LENGTH_LONG).show();

			} else {
				Toast.makeText(getApplicationContext(),
						"Errorr in saving Data", Toast.LENGTH_LONG).show();

			}

		}

	}

}
